支線任務-1
如果我今天是個處理文書的小弟
收到老闆的命令 : 把所有乘客的單獨取出來 , 做成一份檔案
那這時我該怎麼做呢?
可以先大至上找到欄位 : Name 的共通性 -> 在名子後方都會有一個 "," 符號
這時候我們就可以使用GREP套件 快速處理
首先我們先找出 "," 的位置
point = regexpr(",",raw$Name)
> regexpr(",",raw$Name)
[1] 7 8 10 9 6 6 9 8 8 7 10 8 12 10 8 8 5 9 14 11 7 8 8 7 8 8 5 8
[29] 8 9 10 8 6 8 6 10 6 5 14 14 6 7 7 8 8 7 7 11 7 15 7 10 7 11 6 8
找出每個字串點的位置之後
來將我們想要的字串取出
substr(raw$Name,1,point)
[883] "Dahlberg," "Banfield," "Sutehall,"
[886] "Rice," "Montvila," "Graham,"
[889] "Johnston," "Behr,"
目前看起來差了一點 , 多取了一個位置
substr(raw$Name,1,point-1)
完美取出!
[871] "Balkic" "Beckwith" "Carlsson"
[874] "Vander Cruyssen" "Abelson" "Najib"
[877] "Gustafsson" "Petroff" "Laleff"
[880] "Potter" "Shelley" "Markun"
[883] "Dahlberg" "Banfield" "Sutehall"
[886] "Rice" "Montvila" "Graham"
[889] "Johnston" "Behr" "Dooley"
當然我們可以貼心一點 , 把這些名字按照字母順序排序
all_name = substr(raw$Name,1,point-1)
提供兩個做法 :
all_name[order(all_name)]
sort(all_name)
交卷~